<%--
Created by IntelliJ IDEA.
User: 陪你度过漫长岁月
Date: 2022/8/5
Time: 11:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>商品列表</title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>

    <style type="text/css">
        .layui-form-checkbox i {
            position: absolute;
            right: 0;
            top: 27;
            width: 30px;
            height: 28px;
            border: 1px solid #d2d2d2;
            border-left: none;
            border-radius: 0 2px 2px 0;
            color: #fff;
            font-size: 20px;
            text-align: center
        }

        .btn1 {
            margin-top: 19px;
            margin-right: 5px;
        }
    </style>


</head>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" class="layui-table" lay-filter="role-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power"><i
            class="layui-icon layui-icon-vercode"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="category-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="user-enable" {{
           d.enable== true ? 'checked' : '' }} />
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<div align="center">
    <%--    layui 禁用按钮属性 layui-btn-disabled--%>
</div>

<script type="text/html" id="imgtmp">
    <img class="img" src="{{d.pic}}" width="50px" height="50px">
</script>

<script type="text/html" id="radioTab">
    <input type="radio">
    <input type="radio">
</script>

<script type="text/html" id="parent">
    {{d.parentId == 0 ? '一级' : '二级'}}
</script>

<script type="text/html" id="subclass">
    <button class="pear-btn pear-btn-primary pear-btn-md btn1" lay-event="add" onclick="subclass('{{d.id}}')">查看下级
    </button>
</script>

<script type="text/html" id="superior">
    <button class="pear-btn pear-btn-primary pear-btn-md btn1" lay-event="add" onclick="superiorl('0')">返回上级</button>
</script>

<script>
    function subclass(id) {
            layui.use(['table', 'form', 'jquery', 'common'], function () {
                let table = layui.table;

                let cols = [
                    [
                        {type: 'checkbox', title: '选择', width: 80, style: "", sort: false}
                        , {field: 'id', title: '编号', width: 80, sort: true}
                        , {field: 'name', title: '类别名称', width: 150, sort: false}
                        , {field: 'pic', title: '图片', templet: '#imgtmp', width: 150, sort: false}
                        , {field: 'parentId', title: '级别', templet: '#parent', width: 120, sort: true}
                        , {field: 'id', title: '操作', templet: id == '0' ? '#subclass' : '#superior', width: 120, sort: true}
                        , {field: 'sort', title: '排序', width: 80, sort: false}
                        , {field: 'isShow', title: '是否显示', templet: '#category-enable', width: 120, sort: true}
                        , {field: 'createTime', title: '创建时间', width: 180, sort: true}
                        , {field: 'updateTime', title: '更新时间', width: 100, sort: false}
                    ]
                ]

                table.render({
                    elem: '#role-table',
                    url: '${pageContext.request.contextPath}/CategoryListServlet?id=' + id,
                    page: true,
                    cols: cols,
                    skin: 'line',
                    async: true,
                    headers: {
                        token: localStorage.token
                    },
                    request: {
                        pageName: 'pageNo',
                        limitName: 'pageSize',
                    },
                    toolbar: '#role-toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter', 'print', 'exports']
                });
            })
    }

    function superior() {
        layui.use(['table', 'form', 'jquery', 'common'], function () {
            let table = layui.table;

            let cols = [
                [
                    {type: 'checkbox', title: '选择', width: 80, style: "", sort: false}
                    , {field: 'id', title: '编号', width: 80, sort: true}
                    , {field: 'name', title: '类别名称', width: 150, sort: false}
                    , {field: 'parentId', title: '级别', templet: '#parent', width: 120, sort: true}
                    , {field: 'id', title: '操作', templet: id == '0' ? '#subclass' : '#superior', width: 120, sort: true}
                    , {field: 'sort', title: '排序', width: 80, sort: false}
                    , {field: 'isShow', title: '是否显示', templet: '#category-enable', width: 120, sort: true}
                    , {field: 'createTime', title: '创建时间', width: 180, sort: true}
                    , {field: 'updateTime', title: '更新时间', width: 100, sort: false}
                ]
            ]

            table.render({
                elem: '#role-table',
                url: '${pageContext.request.contextPath}/CategoryListServlet?id=' + id,
                page: true,
                cols: cols,
                skin: 'line',
                async: true,
                headers: {
                    token: localStorage.token
                },
                request: {
                    pageName: 'pageNo',
                    limitName: 'pageSize',
                },
                toolbar: '#role-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
        })
    }
</script>

<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "operate/";

        let cols = [
            [
                {type: 'checkbox', title: '选择', width: 80, style: "", sort: false}
                , {field: 'id', title: '编号', width: 80, sort: true}
                , {field: 'name', title: '类别名称', width: 150, sort: false}
                // , {field: 'pic', title: '图片', templet: '#imgtmp', width: 150, sort: false}
                , {field: 'parentId', title: '级别', templet: '#parent', width: 120, sort: true}
                , {field: 'id', title: '操作', templet: '#subclass', width: 120, sort: true}
                , {field: 'sort', title: '排序', width: 80, sort: false}
                , {field: 'isShow', title: '是否显示', templet: '#category-enable', width: 120, sort: true}
                , {field: 'createTime', title: '创建时间', width: 180, sort: true}
                , {field: 'updateTime', title: '更新时间', width: 100, sort: false}
            ]
        ]

        table.render({
            elem: '#role-table',
            url: '${pageContext.request.contextPath}/CategoryListServlet',
            page: true,
            cols: cols,
            skin: 'line',
            async: true,
            headers: {
                token: localStorage.token
            },
            request: {
                pageName: 'pageNo',
                limitName: 'pageSize',
            },
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });


        table.on('tool(role-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'power') {
                window.power(obj);
            }
        });

        table.on('toolbar(role-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(role-query)', function (data) {
            table.reload('role-table', {where: data.field})
            return false;
        });

        form.on('switch(role-enable)', function (obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: "新增",
                closeBtn: 1,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '400px'], scrollbar: false,
                skin: 'layui-layer-rim my-layer1',
                shadeClose: false,
                content: MODULE_PATH + 'add.jsp',
                offset: '10px',
                end: function () {
                }
            });
        }

        window.power = function (obj) {
            layer.open({
                type: 2,
                title: '授权',
                shade: 0.1,
                area: ['320px', '400px'],
                content: MODULE_PATH + 'edit.jsp'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '400px'],
                content: MODULE_PATH + 'edit.jsp'
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该类别', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['roleId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].roleId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些用户', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('role-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            table.reload('role-table');
        }
    })
</script>
</body>
</html>
